class PersonalManagementController < ApplicationController
  layout "home"
    def login
      session[:user_id] = nil
      if request.post?
        if params[:name]==nil || params[:password]==nil
          flash[:notice] = "用户名和密码不可以为空！"
        else
          user = User.authenticate(params[:name], params[:password])
          if user
            session[:user_id] = user.id
            session[:user_name] = user.name
            redirect_to :controller=>"home", :action=>"main", :id=> user.id
          else
            flash[:notice] = "用户名和密码错误！"
            #        redirect_to :controller=>"home", :action=>"index"
          end
        end
      end
    end
    
    def register_save
      @user = User.new(params[:user])
      @user_info = UserInfo.new(:city_id => params[:d_city][:id],:user_id => @user.id)
      @user_detail = UserDetail.new(:user_id => @user.id)
      @user.user_info = @user_info  
      @user.user_detail = @user_detail
   
      respond_to do |format|
        if (@user.save)    #  &&@user.user_detail.save
          flash[:notice] = '用户注册成功.'
          format.html {  redirect_to(:controller=>"personal_management", :action=>"welcome", :id=> @user.id) }
           # format.xml  { render :xml => @user, :status => :created, :location => @user }
        else
          format.html { render :action => "register" }
          format.xml  { render :xml => @user.errors, :status => :unprocessable_entity }
        end
      end
  end
  # GET /users/1
  # GET /users/1.xml
  def detail
      @user = User.find(params[:id])
      @user_detail = @user.user_detail || UserDetail.new(:user_id =>params[:id])
#      flash[:detail_need] = @user.id
    #puts flash[:detail_need]
    rescue ActiveRecord::RecordNotFound
#      @user_detail = UserDetail.new(:user_id =>params[:id])
      flash[:notice] = '用户没有找到'    
  end
  
  def detail_update
    @user = User.find(params[:id])
    @user_detail = @user.user_detail || UserDetail.new(:user_id =>params[:id])
    respond_to do |format|
       if (@user_detail.update_attributes(params[:user_detail]) &&
          @user_detail.update_attributes(
                                        :sex_id => params[:dsex],
                                        :marriage_id => params[:dmarriag],
                                        :qq_secrecy_id => params[:d_secrecy_type1],
                                        :msn_secrecy_id => params[:d_secrecy_type2]
                                        )
         )
        flash[:notice] = '用户详细信息更新成功.'
        format.html { redirect_to(:controller=>"home", :action=>"welcome") }
        format.xml  { head :ok }
      else
        format.html { render :action => "detail" }
        format.xml  { render :xml => @user.errors, :status => :unprocessable_entity }
      end
    end
  end
  
  def welcome
  end

# GET /user_infos/1/edit
  def edit  
#    ToDo if refresh, don't get data.
    @user = User.find(params[:id])
    @user_info = UserInfo.find(:first,:conditions=>["user_id=?",@user.id]) 
  end
  
  
  # PUT /user_infos/1
  # PUT /user_infos/1.xml
  def update
    @user = User.find(params[:id],:include =>'user_info') 
    @user_info = UserInfo.find(:first,:conditions=>["user_id=?",@user.id]) 
    
    #Validate start
    #    session[:user_info_errors]=nil
    #    @user_info.errors.clear 
    if ((params[:user][:email]=~(/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/))==nil)&&(params[:user][:email]!="")
      @user_info.errors.add("emailError"," 邮箱不合法")
    end
    if ((params[:user_info][:postalcode]=~(/[1-9]\d{5}(?!\d)/))==nil)&&(params[:user_info][:postalcode]!="")
      @user_info.errors.add("post"," 邮编不合法")
    end
    if ((params[:user_info][:telephone]=~(/\d{3}-\d{8}|\d{4}-\d{7}/))==nil)&&((params[:user_info][:telephone]=~(/[1-9][0-9]{10,}/))==nil)&&(params[:user_info][:telephone]!="")
      @user_info.errors.add("phone"," 电话号码不合法 ")
    end
    
    if @user_info.errors.length >0
      respond_to do |format|
        session[:user_info_errors]=@user_info
        format.html {redirect_to(:controller=>"personal_management",:action =>"edit")}
      end
    else
      #Validate end
      respond_to do |format|
        if @user.update_attributes(params[:user])&&@user.user_info.update_attributes(params[:user_info])
          flash[:notice] = '基本详细更新成功.'
          format.html { redirect_to(:controller=>"home", :action=>"welcome") }
          format.xml  { head :ok }
        else
          format.html { render :action => "edit" }
          format.xml  { render :xml => @user.errors, :status => :unprocessable_entity }
        end
      end
    end
  end
  
end